package com.telnetnz.ticketdesk.service;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Element;

import org.apache.log4j.Logger;

import com.telnetnz.ticketdesk.bean.Tag;
import com.telnetnz.ticketdesk.bean.TicketToUser;
import com.telnetnz.ticketdesk.bean.User;
import com.telnetnz.ticketdesk.common.config.SpringContext;
import com.telnetnz.ticketdesk.dao.entity.TicketToUserEntity;
import com.telnetnz.ticketdesk.dao.wrapper.TicketToUserWrap;

public class TicketToUserService
{
	private static Logger logger = Logger.getLogger(TicketToUserService.class);
	private static String CACHE_KEY = "id_";
	private static String CACHE_KEY_TK = "tk_";
	private static String CACHE_KEY_US = "us_";

	/**
	 * Important: This is the only entry to get the project bean, including the methods that return instances list
	 * 
	 * @param uid
	 * @return
	 */
	public static TicketToUser getTicketToUser(long ticketId, long userId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY + ticketId + "_" + userId);
		TicketToUser bean = null;
		if (elm == null)
		{
			TicketToUserEntity entity = getWrap().selectByTicketIdAndUserId(ticketId, userId);
			if (entity != null)
			{
				bean = TicketToUser.adapt(entity);
				getWrap().getCache().put(new Element(CACHE_KEY + ticketId + "_" + userId, bean));
			}
		}
		else
		{
			bean = (TicketToUser) elm.getValue();
		}
		return bean;
	}

	public static void add(long ticketId, long userId)
	{
		getWrap().insert(ticketId, userId);
		getWrap().getCache().remove(CACHE_KEY + ticketId + "_" + userId);
		getWrap().getCache().remove(CACHE_KEY_TK + ticketId);
		getWrap().getCache().remove(CACHE_KEY_US + userId);
	}

	public static void delete(long ticketId, long userId)
	{
		getWrap().deleteTicketToUser(ticketId, userId);
		getWrap().getCache().remove(CACHE_KEY + ticketId + "_" + userId);
		getWrap().getCache().remove(CACHE_KEY_TK + ticketId);
		getWrap().getCache().remove(CACHE_KEY_US + userId);
	}

	public static List<User> getUsersByTicketId(long ticketId)
	{
		List<TicketToUser> ttus = getByTicketId(ticketId);
		List<User> users = new ArrayList<User>();
		for (TicketToUser ttu : ttus)
			users.add(ttu.getUser());
		return users;
	}

	public static List<TicketToUser> getByTicketId(long ticketId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY_TK + ticketId);
		List<TicketToUser> bean;
		if (elm == null)
		{
			List<TicketToUserEntity> entities = getWrap().selectByTicketId(ticketId);
			bean = adaptList(entities);
			getWrap().getCache().put(new Element(CACHE_KEY_TK + ticketId, bean));
		}
		else
		{
			bean = (List<TicketToUser>) elm.getValue();
		}
		return bean;
	}

	public static void deleteByTicketId(long ticketId)
	{
		// Delete the relevant cache of this associations before the main deletion, otherwise the relevance can not be retrieved.
		List<TicketToUser> ttus = getByTicketId(ticketId);
		for (TicketToUser ttu : ttus)
			getWrap().getCache().remove(CACHE_KEY_US + ttu.getUserId());

		// Now delete the association and its cache
		getWrap().deleteByTicketId(ticketId);
		getWrap().getCache().remove(CACHE_KEY_TK + ticketId);
	}

	public static List<TicketToUser> getByUserId(long userId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY_US + userId);
		List<TicketToUser> bean;
		if (elm == null)
		{
			List<TicketToUserEntity> entities = getWrap().selectByUserId(userId);
			bean = adaptList(entities);
			getWrap().getCache().put(new Element(CACHE_KEY_US + userId, bean));
		}
		else
		{
			bean = (List<TicketToUser>) elm.getValue();
		}
		return bean;
	}

	public static void deleteByUserId(long userId)
	{
		// Delete the relevant cache of this associations before the main deletion, otherwise the relevance can not be retrieved.
		List<TicketToUser> ttus = getByUserId(userId);
		for (TicketToUser ttu : ttus)
			getWrap().getCache().remove(CACHE_KEY_TK + ttu.getTicketId());

		getWrap().deleteByUserId(userId);
		getWrap().getCache().remove(CACHE_KEY_US + userId);
	}

	private static List<TicketToUser> adaptList(List<TicketToUserEntity> entities)
	{
		List<TicketToUser> beans = new ArrayList<TicketToUser>();
		if (entities != null && entities.size() > 0)
		{
			for (TicketToUserEntity entity : entities)
			{
				TicketToUser bean = getTicketToUser(entity.getTicketId(), entity.getUserId());
				if (bean != null)
					beans.add(bean);
			}
		}
		return beans;
	}

	private static TicketToUserWrap getWrap()
	{
		return SpringContext.getTicketToUserWrap();
	}

	public static void main(String[] args)
	{
		
	}
}
